<template>
  <div class="panel admin-panel">
    <div class="panel-head" id="add">
      <strong>
        <span class="icon-pencil-square-o"></span>增加管理员
      </strong>
    </div>
    <div class="body-content">
      <form method="post" class="form-x" action onsubmit="return false">
        <div class="form-group">
          <!-- 名称 -->
          <div class="label">
            <label>账户：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" v-model="user.account" ref="account" />
            <div class="tips">{{msg.acc}}</div>
          </div>
          <br />
          <br />
          <br />
          <!-- 父id -->
          <div class="label">
            <label>密码：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" v-model="user.password" ref="password" />
            <div class="tips">{{msg.psw}}</div>
          </div>
          <br />
          <br />
          <br />
          <!-- 首页展示 -->
          <div class="label">
            <label>状态：</label>
          </div>
          <div class="field">
            <el-radio-group v-model="user.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label></label>
          </div>
          <div class="field">
            <button class="button bg-main icon-check-square-o" @click="send">提交</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.withCredentials = true;
export default {
  name: "AdminAdd",
  data() {
    return {
      user: {
        account: "",
        password: "",
        status: 1
      },
      msg: {
        acc: "",
        psw: ""
      },
      reg: /\w{6,16}/
    };
  },
  methods: {
    //发送更改信息
    send() {
      if (this.user.account.trim().length < 1) {
        this.msg.acc = "用户名不为空！";
        this.$refs.account.focus();
        return;
      }
      if (!this.reg.test(this.user.password.trim())) {
        this.msg.psw = "密码有字符串数字下划线组成，长度为6-16";
        this.$refs.password.focus();
        return;
      }

      // axios({
      //   method: "post",
      //   headers: {
      //     "Content-Type": "application/json"
      //   },
      //   url: this.host + "/admin/add",
      //   data: JSON.stringify(this.user),
      //   withCredentials:true
      // })
      axios
      .post(this.host + "/admin/add",this.user)
        .then(r => {
          if (r.data.code == "200") {
            this.suc("添加成功!");
          } else {
            this.error(r.data.message);
          }
        })
        .catch(r => {
          this.error("保存出错!");
        });
    }
  }
};
</script>

<style scoped>
</style>
